(function() {
  app.controller('zixuanCtrl', function($state, $scope, $rootScope, quoteApi, header_m, my_zxg_v, tradeApi, $interval, local_m, toast) {
    header_m.set({
      css_l: {
        name: 'icon-write'
      },
      fun_l: function() {
        $state.go('zixuan_m');
      },
      tab_show: true,
      tabs: [{
        txt: '自选',
        go: '/zixuan'
      }, {
        txt: '市场',
        go: '/market.hs'
      }],
      tab_go: function(it) {
        $state.go(local_m.get('market_tab_index') || 'market.hs');
      },
      css_r: {
        name: 'icon-search'
      },
      fun_r: function() {
        $state.go('search', { type: 'zixuan' });
      },
    });

    local_m.set('type_yiyuan', false);
    $scope.init = function() { // 行情数据请求
      my_zxg_v
        .updata()
        .then(function(res) {
          var d = new quoteApi.QuoteApi.ZxgRT0x20({ // tab里明细
            stocks: res,
            sublist: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 34, 35]
          });
          quoteApi.query(d).then(function(data) {
            $scope.hangqing_list = angular.isArray(data) ? data : [data];
          });
        });
    };
    var timer = $interval(function() {
      $scope.init();
    }, 3000);
    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
      clearInterval(window.toastSetinterval);
    });
    $scope.go_detail = function(item) {
      $scope.storage.local('detail_codeinfo', item.stkinfo);
      if (item.stkinfo.style === 2) {
        $state.go('detail');
      } else if (item.stkinfo.style === 1) {
        $state.go('detail_zs');
      }
    };
    $scope.sort_click = function($event, param) { // 排序函数 param为要排序的字段
      var target = angular.element($event.target);
      if (target.hasClass('sort_up')) {
        target.addClass('sort_down');
        target.removeClass('sort_up');
        $scope.order_key = '-' + param; // 排序
      } else if (target.hasClass('sort_down')) {
        target.removeClass('sort_down');
        target.addClass('sort_up');
        $scope.order_key = param;
      } else {
        target.addClass('sort_up');
        $scope.order_key = param;
      }
      angular.forEach(target.siblings(), function(item) {
        angular.element(item).removeClass('sort_up');
        angular.element(item).removeClass('sort_down');
      }, this);
    };
  });
  app.filter('add_symbol', function() {
    return function(input) {
      if (input > 0) return '+' + input;
      return input;
    };
  });
})();